package com.example.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JokerJodas
 * @date 2018.6.11
 */
public class ResponseData {
    private static Logger logger = LoggerFactory.getLogger(ResponseData.class);
    /**
     * 仅反馈成功，无详细数据，请用这个
     */
    public static final ResponseData SUCCESS = new ResponseData(CommResponseEnum.SUCCESS);

    /**
     * 未知错误，无详细数据，请用这个
     */
    public static final ResponseData FAILURE = new ResponseData(CommResponseEnum.FAILURE);

    public static final ResponseData FAILURE1 = new ResponseData(CommResponseEnum.FAILURE1);

    private int code;
    private String msg;
    private Object data;

    /**
     * 异常码反馈
     *
     * @param responseEnum
     */
    public ResponseData(IResponseEnum responseEnum) {
        this.code = responseEnum.code();
        this.msg = responseEnum.msg();
    }

    /**
     * 成功消息
     *
     * @param data
     */
    public ResponseData(Object data) {
        this(CommResponseEnum.SUCCESS, data);
    }

    /**
     * 异常时还需要数据的情况
     *
     * @param responseEnum
     * @param data
     */
    public ResponseData(IResponseEnum responseEnum, Object data) {
        this.code = responseEnum.code();
        this.msg = responseEnum.msg();
        this.data = data;
    }

    /**
     * 成功消息
     *
     * @param map
     */
    public static String turnResponse(Object map) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        if (map instanceof List) {
            if(((List)map).size() == 0){
                resMap.put("code", "600");
                resMap.put("msg", "查无数据");
            }else{
                resMap.put("code", "200");
                resMap.put("msg", "操作成功");
                resMap.put("data", map);
            }
        } else {
            resMap.put("code", "200");
            resMap.put("msg", "操作成功");
            resMap.put("data", map);
        }
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        String result = mapper.writeValueAsString(resMap);
        logger.info("返回结果：" + result);
        return result;
    }

    /**
     * 成功消息
     *
     * @param
     */
    public static String OKResponse() throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("code", "200");
        resMap.put("msg", "操作成功");
        String result = new ObjectMapper().writeValueAsString(resMap);
        logger.info("返回结果：" + result);
        return result;
    }

    /**
     * 失败消息，但是不回滚事务
     *
     * @param
     */
    public static String FiledResponse(String msg) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("code", 500);
        resMap.put("msg", msg);
        return new ObjectMapper().writeValueAsString(resMap);
    }

    /**
     * 操作失败，返回自定义失败信息
     *
     * @param o
     * @return
     */
    public static String fail(Object o) {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("code", CommResponseEnum.FAILURE1.code());
        resMap.put("msg", o);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        String result = null;
        try {
            result = mapper.writeValueAsString(resMap);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        logger.info("返回结果： {}", result);
        return result;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}


